package com.ac.util;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;

import com.ac.common.Constants;
import com.ac.entity.Parts;
import com.ac.entity.PartsType;

public class CloneUtil {
	
	public static Object cloneObect(Object orig){
		try {
			return  BeanUtils.cloneBean(orig);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	public static void clonePartsTypeList(List<PartsType> dest,List<PartsType>orig){
		
		for (PartsType object : orig) {
			try {
				PartsType pt = (PartsType) BeanUtils.cloneBean(object);
				dest.add(pt);
				pt.setPartsList(new ArrayList<Parts>());
				if(object.getPartsList() != null){
					for (int i = 0; i < object.getPartsList().size(); i++) {
						pt.getPartsList().add((Parts) BeanUtils.cloneBean(object.getPartsList().get(i)));
					}
				}
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static List<PartsType> convertToPartsTypeList(String partsUnion,String split1){
		List<PartsType> list = new ArrayList<PartsType>();
		if(partsUnion == null || partsUnion.equals("")){
			return list;
		}
		String[] partsArray = StringUtils.split(partsUnion, split1);
		for (String parts : partsArray) {
			for (PartsType partsTypeTemp : DataStore.getPartsTypeList()) {
				for (Parts partsTemp : partsTypeTemp.getPartsList()) {
					if(parts.equals(partsTemp.getId()+"")){
						boolean partsTypeFlag = false;
						for(PartsType partsTypeTemp2 : list){
							if(partsTypeTemp2.getId() == partsTypeTemp.getId()){
								partsTypeFlag = true;
								partsTypeTemp2.getPartsList().add((Parts) cloneObect(partsTemp));
								break;
							}
						}
						if(!partsTypeFlag){
							PartsType newPartsType =(PartsType) cloneObect(partsTypeTemp);
							newPartsType.setPartsList(new ArrayList<Parts>());
							newPartsType.getPartsList().add((Parts) cloneObect(partsTemp));
							list.add(newPartsType);
							
						}
					}
				}
			}
		}
		return list;
	}
	
	public static List<PartsType> convertToPartsTypeList(String partsUnion,String split1,String split2){
		List<PartsType> list = new ArrayList<PartsType>();
		
		if(partsUnion == null || partsUnion.equals("")){
			return list;
		}
		
		CloneUtil.clonePartsTypeList(DataStore.getPartsTypeList(), list);
		
		String[] unionArray = StringUtils.split(partsUnion, split1);
		for (String string : unionArray) {
			String[] partsArray = StringUtils.split(string,split2);
				
			for (PartsType partsTypeTemp : list) {
				for (Parts partsTemp : partsTypeTemp.getPartsList()) {
					if(!partsArray[0].equals(partsTemp.getId()+"")){
						partsTypeTemp.getPartsList().remove(partsTemp);
					}
				}
				
				if(partsTypeTemp.getPartsList().size() == 0){
					list.remove(partsTypeTemp);
				}
			}
		}
		return list;
	}
	
}
